#include "settingdialog.h"
#include "ui_settingdialog.h"
#include <QtWidgets>

SettingDialog::SettingDialog(QWidget *parent, QString user, QString ip, QString password, QString log_file) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    can_be_load = false;
    ssh_user = user;
    ssh_ip = ip;
    ssh_password = password;
    dhcp_log_file = log_file;
    ui->setupUi(this);
    QString ipRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    QRegExp ipRegex("^" + ipRange+ "\\." + ipRange+ "\\." + ipRange+ "\\." + ipRange + "$");
    ipValidator = new QRegExpValidator(ipRegex, this);
    ui->sshline->setValidator(ipValidator);
    ui->passline->setEchoMode(QLineEdit::Password);
    ui->sshline->setText(ssh_ip);
    ui->userline->setText(ssh_user);
   // ui->passlable->setText(ssh_password);
    ui->dhcpline->setText(dhcp_log_file);
}

SettingDialog::~SettingDialog()
{
    delete ui;
}

QString SettingDialog::get_ssh_user()
{
    return ssh_user;
}

QString SettingDialog::get_ssh_ip()
{
    return ssh_ip;
}

QString SettingDialog::get_ssh_password()
{
    return ssh_password;
}

QString SettingDialog::get_dhcp_log_file()
{
    return dhcp_log_file;
}

void SettingDialog::on_savebutton_clicked()
{
    if((ui->userline->text().isEmpty())||(ui->sshline->text().isEmpty()) || (ui->dhcpline->text().isEmpty())){
        QMessageBox::critical(this, tr("Empty Field!"), tr("All the SSH USER , IP,  LOG_FILE editline should not be "
                                                           "<b> empty</b>!"));
        return;
    }
    QFileInfo finfo(ui->dhcpline->text());
    if(!finfo.isFile()){
        QMessageBox::critical(this, tr("Error While Testing dhcpd log file!"), tr("The dhcpd log file doesn't name a file!<br>"
                                                                                  "Please input the corrent file name or using the browse button to select"
                                                                                  "a file!"));
        return;
    }
    ssh_user = ui->userline->text();
    ssh_ip = ui->sshline->text();
    if(!ui->passline->text().isEmpty())
        ssh_password = ui->passline->text();
    dhcp_log_file = ui->dhcpline->text();
    can_be_load = true;
    this->close();
}

void SettingDialog::on_cancelbutton_clicked()
{
    can_be_load = false;
    this->close();
}

void SettingDialog::on_browsebutton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Get DHCP Log File Name"), tr("Log File (*.log) ;; All File (*)"));
    if(!fileName.isEmpty())
        ui->dhcpline->setText(fileName);
}
